<template>
  <div>
    <!-- 搜索框 -->
    <div style="padding: 10px 0">
      <el-input style="width: 200px;" suffix-icon="el-icon-search" placeholder="请输入用户名"
                v-model="username"></el-input>
      <el-button type="primary" style="margin-left: 5px" @click="findAll">搜索<i class="el-icon-loading"></i>
      </el-button>
      <el-button type="warning" style="margin-left: 5px" @click="reset">重置<i class="el-icon-loading"></i>
      </el-button>
    </div>

    <!-- 按钮 -->
    <div style="margin: 10px 0;">
      <el-button type="primary" style="margin-right: 5px" @click="insertBtn">新增<i class="el-icon-circle-plus-outline"></i></el-button>
      <el-popconfirm
          confirm-button-text='确定'
          cancel-button-text='再想想'
          icon="el-icon-info"
          icon-color="red"
          title="您确定要删除这些用户吗？"
          @confirm="deleteUserBatch"
      >
        <el-button slot="reference" type="danger"><i class="el-icon-remove-outline"></i>批量删除</el-button>
      </el-popconfirm>

      <el-button type="primary" style="margin: 0 5px">导入<i class="el-icon-bottom"></i></el-button>
      <el-button type="primary">导出<i class="el-icon-top"></i></el-button>
    </div>

    <el-table :data="tableData" border :header-cell-class-name="headerBg" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="ID" width="100"></el-table-column>
      <el-table-column prop="username" label="用户名" width="120"></el-table-column>
      <el-table-column prop="nickname" label="昵称" width="120"></el-table-column>
      <el-table-column prop="email" label="邮箱" width="120"></el-table-column>
      <el-table-column prop="phone" label="电话" width="120"></el-table-column>
      <el-table-column prop="address" label="地址" width="300"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="success" style="margin-right: 5px"><i
              class="el-icon-edit"></i>编辑
          </el-button>
          <el-popconfirm
              confirm-button-text='确定'
              cancel-button-text='再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定要删除该用户吗？"
              @confirm="delUserById(scope.row.id)"
          >
            <el-button slot="reference" type="danger"><i class="el-icon-remove-outline"></i>删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>


    <el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="userForm" label-width="80px" size="small">
        <el-form-item label="用户名">
          <el-input v-model="userForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="userForm.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="userForm.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="userForm.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="userForm.address" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2,5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "User",
  data() {
    return {
      tableData: [],
      pageNum: 1,
      pageSize: 5,
      total: 0,
      headerBg: 'headerBg',
      username: '',
      dialogFormVisible: false,
      userForm: {},
      multipleSelection: []
    }
  },
  created() {
    this.findAll();
  },
  methods: {
    findAll() {
      axios.get("http://localhost:9090/user/page?pageNum=" + this.pageNum + "&pageSize=" + this.pageSize + "&username=" + this.username + "").then(res => {
        console.log(res)
        this.tableData = res.data.data;
        this.total = res.data.total;
      })
    },
    // 保存
    save() {
      axios.post("http://localhost:9090/user/save", this.userForm).then(res => {
        console.log(res)
        if (res.data === 1) {
          this.$message.success("保存成功")
        }
        this.dialogFormVisible = false;
        this.findAll();
      })
    },
    delUserById(id) {
      axios.delete("http://localhost:9090/user/delete/" + id).then(res => {
        console.log(res)
        if (res.data === 1) {
          this.$message.success("删除成功")
        } else {
          this.$message.error("删除失败")
        }
        this.findAll();
      })
    },
    // 批量删除
    deleteUserBatch() {
      let ids = this.multipleSelection.map(v => v.id)
      axios.post("http://localhost:9090/user/delete/batch",ids).then(res =>{
        console.log(res)
        if (res.data === true) {
          this.$message.success("批量删除成功")
        }
        this.findAll();
      })
    },
    // 重置
    reset() {
      this.username = '';
      this.findAll();
    },
    insertBtn() {
      this.userForm = {};
      this.dialogFormVisible = true
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val;
    },
    collapse() {
      this.isCollapse = !this.isCollapse;
      if (this.isCollapse) {
        this.sideWidth = 64;
        this.collapseBtnClass = 'el-icon-s-unfold';
        this.logoTextShow = false;
      } else {
        this.sideWidth = 200;
        this.collapseBtnClass = 'el-icon-s-fold'
        this.logoTextShow = true;
      }
    },
    handleClick(row) {
      console.log(row);
      this.dialogFormVisible = true;
      this.userForm = row
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.findAll();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val;
      this.findAll();
    }
  }
}
</script>

<style>
.headerBg {
  background-color: #CCCCCC !important;
}
</style>
